﻿using fastdb.Lexer;

namespace fastdb.Parser;

public class ParserCursor
{
    private readonly List<TokenNode> _tokens;
    private int _pos = -1;

    public ParserCursor(List<TokenNode> tokens)
    {
        _tokens = tokens;
    }

    public void SetPos(int pos)
    {
        _pos = pos;
    }

    public int GetPos()
    {
        return _pos;
    }

    private TokenNode GetAt(int pos)
    {
        if (pos < 0 || pos >= _tokens.Count) return new TokenNode();
        return _tokens[pos];
    }

    public TokenNode GetCurrent()
    {
        return GetAt(_pos);
    }

    public TokenNode GetPre()
    {
        return GetAt(_pos - 1);
    }

    public TokenNode GetNext()
    {
        return GetAt(_pos + 1);
    }

    public TokenNode GetByPos(int pos)
    {
        return GetAt(pos);
    }

    public TokenNode MoveNext()
    {
        if (_pos < _tokens.Count - 1)
        {
            _pos++;
            return GetAt(_pos);
        }

        return null;
    }

    public TokenNode MovePre()
    {
        if (_pos > 0)
        {
            _pos--;
            return GetAt(_pos);
        }

        return null;
    }
}